package cn.tradewin.advisor.pages.account;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.services.AccountService;
import cn.tradewin.advisor.persist.services.AdvisorSecurityService;

public class AccountBoard {
	
	@Inject
	private Messages messages;
	
	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<Account> beanModel;
	
	@Inject
	private AccountService accountService;
	
	@Inject
	private AdvisorSecurityService securityService;
	
	@Property
	private List<Account> accounts;
	
	@Property
	private Account account;
	
	@SetupRender
	void setupRender() throws DBException {
		beanModel = beanModelSource.createDisplayModel(Account.class, messages);
		beanModel.add("action", null);
		beanModel.include("id", "userName", "email", "mobile", "jobName", "action");
		for (String propertyName : beanModel.getPropertyNames()) {
			beanModel.get(propertyName).sortable(false);
		}
	}	
	
	@OnEvent(EventConstants.PREPARE_FOR_RENDER)
	void prepareForRender() throws DBException {
		accounts = accountService.listAllAccounts();
	}
	
	void onDelete(String id) throws DBException {
		accountService.lockAccount(id);
	}
}